package com.supersoul.myself.controller;

import com.alibaba.fastjson.JSONObject;
import com.supersoul.myself.cache.TokenCache;
import com.supersoul.myself.entity.Blog;
import com.supersoul.myself.entity.BlogCollection;
import com.supersoul.myself.entity.User;
import com.supersoul.myself.ex.ServiceException;
import com.supersoul.myself.service.BlogService;
import com.supersoul.myself.util.PageVO;
import com.supersoul.myself.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;


@RestController
@RequestMapping("/blog")
public class BlogController {

    @Autowired
    private BlogService blogService;

    @Autowired
    private TokenCache tokenCache;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 点赞
     * @param blogId
     * @return
     * @throws ServiceException
     */
    @GetMapping("/clickLikes/{blogId}")
    public R clickLikes(@PathVariable("blogId") String blogId) throws ServiceException{
        Integer likes = blogService.clickLikes(blogId);
        return R.success(likes);
    }

    /**
     * 取消点赞
     * @param blogId
     * @return
     * @throws ServiceException
     */
    @GetMapping("/cancelLikes/{blogId}")
    public R cancelLikes(@PathVariable("blogId") String blogId) throws ServiceException{
        Integer likes = blogService.cancelLikes(blogId);
        return R.success(likes);
    }

    /**
     * 收藏博客/取消收藏博客
     * @param blogCollection
     * @param request
     * @return
     * @throws ServiceException
     */
    @PostMapping("/collection")
    public R collectBlog(@RequestBody @Valid BlogCollection blogCollection, HttpServletRequest request) throws ServiceException {
        String token = request.getHeader("token");
        String tokenJson = stringRedisTemplate.opsForValue().get(token);
        User user = JSONObject.parseObject(tokenJson, User.class);
        if (user.getId().equals(blogCollection.getCreatorId())) {
            // 用户的id和博客的收藏着id必须一致
            return blogService.collectBlog(blogCollection);
        }
        return R.failed();
    }

    @GetMapping("/collection/list")
    public R getUserBlogCollectionPage(PageVO pageVO, HttpServletRequest request) {
        String token = request.getHeader("token");
        String tokenJson = stringRedisTemplate.opsForValue().get(token);
        User user = JSONObject.parseObject(tokenJson, User.class);
        Page<BlogCollection> userBlogCollectionPage = blogService.getUserBlogCollectionPage(user, pageVO);
        return R.success(userBlogCollectionPage);
    }

    /**
     * 新建/编辑博客
     * @param blog
     * @param request
     * @return
     * @throws ServiceException
     */
    @PostMapping("/save")
    public R saveBlog(@Valid @RequestBody Blog blog, HttpServletRequest request) throws ServiceException {
        String token = request.getHeader("token");
        String tokenJson = stringRedisTemplate.opsForValue().get(token);
        User user = JSONObject.parseObject(tokenJson, User.class);
        String blogId = blogService.saveBlog(blog, user);
        return R.success(blogId);
    }

    /**
     * 删除博客.只能自己删除自己的
     * @param id
     * @param request
     * @return
     * @throws ServiceException
     */
    @GetMapping("/delete/{id}")
    public R delBlogById(@PathVariable("id") @NotEmpty String id, HttpServletRequest request) throws ServiceException {
        String token = request.getHeader("token");
        String tokenJson = stringRedisTemplate.opsForValue().get(token);
        User user = JSONObject.parseObject(tokenJson, User.class);
        blogService.delBlogById(id, user);
        return R.success();
    }

    /**
     * 查看博客
     * @param id
     * @return
     * @throws ServiceException
     */
    @GetMapping("/detail/{id}")
    public R getBlogById(@PathVariable("id") @NotEmpty String id) throws ServiceException {
        Blog blog = blogService.findBlogById(id);
        return R.success(blog);
    }


    /**
     * 查询公开的, 已经发布的所有博客
     * @param pageVO
     * @return
     * @throws ServiceException
     */
    @GetMapping("/public/list")
    public R getBlogPublic(PageVO pageVO, String cgId) throws ServiceException {
        Page<Blog> blogPage = blogService.findBlogPublic(pageVO, cgId);
        return R.success(blogPage);
    }

    /**
     * 查询某个用户博客
     * @param pageVO
     * @return
     * @throws ServiceException
     */
    @GetMapping("/person/list")
    public R getBlogPerson(PageVO pageVO, Integer permissionState, Integer publishState, String cgId, Integer orderId, HttpServletRequest request) throws ServiceException {
        String token = request.getHeader("token");
        String tokenJson = stringRedisTemplate.opsForValue().get(token);
        User user = JSONObject.parseObject(tokenJson, User.class);
        Page<Blog> blogPage = blogService.findBlogPerson(pageVO, permissionState, publishState, cgId, orderId, user);
        return R.success(blogPage);
    }




}
